#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QDebug>
#include <QUrlQuery>
#include <QNetworkProxy>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle(tr("Qt高级网络操作"));
    test();
}

void MainWindow::test()
{
//    // URL
//    QString baseUrl = "http://www.csdn.net/";
//    // 构造请求
//    QNetworkRequest request;
//    request.setUrl(QUrl(baseUrl));
//    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
//    qDebug() << manager->supportedSchemes();
//    // 发送请求
//    QNetworkReply *pReplay = manager->get(request);
//    // 开启一个局部的事件循环，等待响应结束，退出
//    QEventLoop eventLoop;
//    QObject::connect(manager, &QNetworkAccessManager::finished, &eventLoop, &QEventLoop::quit);
//    eventLoop.exec();
//    // 获取响应信息
//    QByteArray bytes = pReplay->readAll();
//    qDebug() << bytes;

//    URL
//    QString baseUrl = "http://www.zhihu.com/search";
//    QUrl url(baseUrl);
//    // key-value 对
//    QUrlQuery query;
//    query.addQueryItem("type", "content");
//    query.addQueryItem("q", "Qt");
//    url.setQuery(query);
//    qDebug() << url.url();
//    "http://www.zhihu.com/search?type=content&q=Qt"

    QNetworkProxy proxy;
    proxy.setType(QNetworkProxy::HttpProxy);
    proxy.setHostName("127.0.0.1");
    proxy.setPort(8888);
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    manager->setProxy(proxy);
}

MainWindow::~MainWindow()
{
    delete ui;
}


